package es.udc.fic.hamNeggs.pojo;

import es.udc.fic.hamNeggs.pojo.UserStories.State;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Entity
@Table(name = "hamneggs.sprints")
public class Sprint {

    @Id
    @GeneratedValue
    @Column(name = "spr_id")
    private Long             id;

    @Column(name = "spr_dateStart")
    private Date             dateStart;

    @Column(name = "spr_name")
    private String           name;

    @Column(name = "spr_dateEnd")
    private Date             dateEnd;

    @ManyToOne
    @JoinColumn(name = "prj_id")
    private Project          project;

    @OneToMany(mappedBy = "sprint")
    private Set<UserStories> proposedTasks;

    public Sprint() {

    }

    public Date getDateStart() {
        return dateStart;
    }

    public void setDateStart(Date dateStart) {
        this.dateStart = dateStart;
    }

    public Date getDateEnd() {
        return dateEnd;
    }

    public void setDateEnd(Date dateEnd) {
        this.dateEnd = dateEnd;
    }

    public Long getId() {
        return id;
    }

    public Project getProject() {
        return project;
    }

    public List<UserStories> getProposedTasks() {
        List<UserStories> list = new ArrayList<UserStories>();
        list.addAll(proposedTasks);

        Collections.sort(list,
                new Comparator<UserStories>() {
                    public int compare(UserStories o1, UserStories o2) {
                        return o1.getId().intValue() - o2.getId().intValue();
                    }
                });
        return list;
    }

    public int getProposedTasksDone() {
        int cuenta = 0;
        for (UserStories story : proposedTasks) {
            if (story.getState().getString().equalsIgnoreCase(State.Validated.getString()))
                cuenta++;
        }
        return cuenta;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    public void setOneProposedTask(UserStories ust) {
        if (proposedTasks == null)
            proposedTasks = new HashSet<UserStories>();
        proposedTasks.add(ust);
    }

    public void setProposedTasks(Set<UserStories> proposedTasks) {
        this.proposedTasks = proposedTasks;
    }

}
